package com.zhangxiaofanfan.hotcode.second;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-24 10:34:02
 */
public class HotCode013 {
    public static void main(String[] args) {
        // int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
        int[] nums = {5,4,-1,7,8};
        HotCode013 hotCode013 = new HotCode013();
        System.out.println(hotCode013.maxSubArray(nums));
    }

    public int maxSubArray(int[] nums) {
        int maxSum = nums[0], sum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            sum = Math.max(sum, 0);
            sum += nums[i];
            maxSum = Math.max(maxSum, sum);
        }
        return maxSum;
    }
}
